#include "DriftManager.h"
#include "DriftControllerInterface.h"
#include "DriftController.h"
#include <Physics/Vehicle/hkpVehicleInstance.h>


DriftManager::DriftManager(void)
{

}


DriftManager::~DriftManager(void)
{
	reclaim_all();
}


DriftControllerInterface* DriftManager::get_drift_controller(hkpVehicleInstance* pVehicleInstance)
{
	DriftControllerInterface* pDriftControllerInterface;
	if (m_container.count(pVehicleInstance)) pDriftControllerInterface = m_container[pVehicleInstance];
	else
	{
		pDriftControllerInterface= new DriftController(pVehicleInstance);
		m_container[pVehicleInstance] = pDriftControllerInterface;
	}
	
	return pDriftControllerInterface;
}


void DriftManager::remove_drift_controller(hkpVehicleInstance* pVehicleInstance)
{
	if(m_container.count(pVehicleInstance))
	{
		delete m_container[pVehicleInstance];
		m_container.erase(pVehicleInstance);
	}
}


void DriftManager::reclaim_all(void)
{
	std::map<hkpVehicleInstance*, DriftControllerInterface*>::iterator iEnd = m_container.end();
	std::map<hkpVehicleInstance*, DriftControllerInterface*>::iterator it = m_container.begin();
	for(; it != iEnd; it++) delete (*it).second;
	m_container.clear();
}